package com.iuhao.web.core.utils;

import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.codec.binary.Base64;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.util.Base64Utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by yangpeng<yangpeng01@vcredit.com>.
 * Date: 2016/9/8
 */

public class ImageUtils {

//    public static void main(String args[]) throws Exception {
//        BufferedImage img1 = ImageIO.read(new File("D:/1.jpg"));
//        BufferedImage img2 = ImageIO.read(new File("D:/2.jpg"));
//        BufferedImage joinedImg = twoImagesIntoOne(img1, img2);
//        ImageIO.write(joinedImg, "jpg", new File("D:/3.jpg"));
//    }

    /**
     * base64压缩
     *
     * @param base64
     * @param w
     * @param h
     * @return
     */
    public static String compressBase64(String base64, int w, int h) {
        String base64Str = null;
        File srcImageFile = null;
        File destImageFile = null;
        try {
            String temp = System.getProperty("java.io.tmpdir") + "/temp/compress/";
            File tempdir = new File(temp);
            if (!tempdir.exists()) {
                tempdir.mkdirs();
            }
            long currentTime = System.currentTimeMillis();
            //源图片
            String srcImage = temp + currentTime + "_src.jpg";
            srcImageFile = new File(srcImage);
            FileOutputStream srcOut = new FileOutputStream(srcImageFile);
            srcOut.write(decodeImage(base64));
            srcOut.close();
            //压缩后的图片
            String destImage = temp + currentTime + "_dest.jpg";
            destImageFile = new File(destImage);
            Thumbnails.of(srcImageFile).size(w, h).toFile(destImageFile);
            BufferedImage img1 = ImageIO.read(destImageFile);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(img1, "jpg", baos);
            byte[] base64Byte = baos.toByteArray();
            base64Str = Base64Utils.encodeToString(base64Byte);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                FileUtils.forceDelete(srcImageFile);
                FileUtils.forceDelete(destImageFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return base64Str;
    }


    public static byte[] decodeImage(String imageDataString) {
        return Base64.decodeBase64(imageDataString);
    }

    public static BufferedImage twoImagesIntoOne(BufferedImage img1, BufferedImage img2) {
        int offset = 20;
        int width = Math.max(img1.getWidth(), img2.getWidth());
        int height = img1.getHeight() + img2.getHeight() + offset;
        BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = newImage.createGraphics();
        Color oldColor = g2.getColor();
        g2.setPaint(Color.white);
        g2.fillRect(0, 0, width, height);
        g2.setColor(oldColor);
        g2.drawImage(img1, null, 0, 0);
        g2.drawImage(img2, null, 0, img1.getHeight() + offset);
        g2.dispose();
        return newImage;
    }


}
